package cn.youframe.frame.ctrl;

import cn.youframe.frame.base.BaseCtrl;
import cn.youframe.frame.beans.Menber;
import cn.youframe.frame.beans.UserInfo;
import cn.youframe.frame.beans.YfDeptVO;
import cn.youframe.frame.beans.YfRoleVO;
import cn.youframe.frame.service.LoginService;
import cn.youframe.frame.util.PassWord;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-4-10 10:20
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class LoginCtrl extends BaseCtrl{
    @Autowired
    LoginService loginService;
    @RequestMapping("/index.do")
    public String index(HttpServletRequest request){
        request.getSession().setAttribute("ctxpath",request.getContextPath());
        return "frame/login";
    }
    @RequestMapping("/login.do")
    public String login(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute("ctxpath", request.getContextPath());
        SecurityContextImpl sci=(SecurityContextImpl)session.getAttribute("SPRING_SECURITY_CONTEXT");
        Authentication authentication=sci.getAuthentication();
        Object cred=authentication.getCredentials();
        WebAuthenticationDetails authDetail=(WebAuthenticationDetails)authentication.getDetails();
        User user=(User)authentication.getPrincipal();
        String account=user.getUsername();
        try{
            Menber menber=loginService.login(account);
            menber.setRemoteAddress(authDetail.getRemoteAddress());//客户端地址
            String serverAddr=request.getServerName()+":"+request.getServerPort();
            menber.setServerAddr(serverAddr);//设置登录服务器
            menber.setSessionId(session.getId());
            SessionListener userInfo=new SessionListener(menber);
            session.setAttribute("userSession", userInfo);
            session.setAttribute("uid",menber.getUid());//用户ID
            session.setAttribute("account",menber.getAccount());//登录账号
            session.setAttribute("did",menber.getDid());//部门ID
            session.setAttribute("password", PassWord.decode(menber.getPassword()));//密码
            session.setAttribute("userName",menber.getUserName());//用户名
            setUserSession(request, menber);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
        }
        return "redirect:frame/main.jsp";
    }

    /**
     * 设置用户Session信息
     * @param request
     * @param menber
     */
    private void setUserSession(HttpServletRequest request,Menber menber){
        UserInfo userInfo=new UserInfo();
        try {
            BeanUtilsBean.getInstance().copyProperties(userInfo,menber);
            YfDeptVO dept=loginService.getDept(menber.getDid());
            userInfo.setDeptVO(dept);//设置用户部门
            List<YfRoleVO> rlist=loginService.getRoles(menber.getUid());
            userInfo.setRoleList(rlist);//设置所属角色
            request.getSession().setAttribute("userInfo",userInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
    }
}
